package com.realtyManage.springboot.Dao;


import com.realtyManage.springboot.Model.Do.Commodity;
import com.realtyManage.springboot.Model.Do.Exchange;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 兑换商品
 *
 * @author 杭州素色网络科技有限公司
 * @since 2019-12-16
 */

@Repository
public interface ExchangeRepository extends JpaRepository<Exchange, Long>, JpaSpecificationExecutor<Exchange> {

    @Query(value = "select * from exchange where id = ?1",nativeQuery = true)
    Exchange findById(String id);

    @Query(value = "select * from exchange where user_id = ?1 order by type ASC",nativeQuery = true)
    List<Exchange> findExchangeList(String id);

    @Modifying
    @Transactional
    @Query("update Exchange a set " +
            "a.userId =  CASE WHEN :#{#exchange.userId} IS NULL THEN a.userId ELSE :#{#exchange.userId} END ," +
            "a.num =  CASE WHEN :#{#exchange.num} IS NULL THEN a.num ELSE :#{#exchange.num} END ," +
            "a.phone =  CASE WHEN :#{#exchange.phone} IS NULL THEN a.phone ELSE :#{#exchange.phone} END ," +
            "a.type =  CASE WHEN :#{#exchange.type} IS NULL THEN a.type ELSE :#{#exchange.type} END " +
            "where a.id = :#{#exchange.id}")
    int updateExchange(@Param("exchange") Exchange exchange);

}
